// 一些操作v-treeview数据的公共方法
// v-treeview绑定的items是顶层数据，子节点放在节点item的children下面
import Vue from "vue"
import merge from "lodash/merge"

class ITreeViewItem {
  constructor() {
    this.id = null
    this.children = [] // empty children array used for v-treeview
    this.name_key = "name"
  }
}
class TreeViewHelper {
  constructor(items) {
    this.top_level_items = items
    if (items.length > 0) {
      this.top_level_parent_id = items[0].parent_id
    } else {
      this.top_level_parent_id = null
    }
  }
  static addChildren(item, children) {
    // 对某节点进行加载children操作
    if (children && children.length > 0) {
      Vue.set(item, 'children', [...children])
      TreeViewHelper.sortItems(item.children)      
    } else {
      // using Vue.delete so the dom can be updated
      Vue.delete(item, 'children') // please for v-treeview on empty children, it should be fixed in upstream
    }
  }

  static sortItems(item_array) {
    // 对array原位排序
    if (item_array) {
      item_array.sort((a, b) => a[a.name_key].localeCompare(b[a.name_key]))
    }
    return item_array
  }
  findItemById(item_id) {
    // 广度优先遍历，找出id对应的item，没找到返回null
    let findItem = this.top_level_items.find((x) => x.id === item_id)
    if (findItem != null) {
      return findItem
    }
    let cur_level_items = this.top_level_items
    // 然后再遍历当前层的子层，一直往下
    while (cur_level_items && cur_level_items.length > 0) {
      let next_level_items = []
      for (const item of cur_level_items) {
        if (item.children && item.children.length > 0) {
          let findItem = item.children.find((x) => x.id === item_id)
          if (findItem != null) {
            return findItem
          } else {
            next_level_items = [...next_level_items, ...item.children]
          }
        }
      }
      cur_level_items = next_level_items
    }
    return null // 找到了会中途返回，没找到就返回null了
  }
  replaceItem(item) {
    // 如果对item进行了操作，想要替换
    let old_item = this.findItemById(item.id)
    old_item = merge(old_item, item)
    return old_item
  }
  insertItem(item) {
    if (item.parent_id === this.top_level_parent_id) {
      // 说明是顶层的目录
      this.top_level_items.push(item)
      TreeViewHelper.sortItems(this.top_level_items)
    } else {
      const parent_node = this.findItemById(item.parent_id)
      if (parent_node) {
        if (!parent_node.children) {
          Vue.set(parent_node, 'children', [item])
        }else{
          parent_node.children.push(item)
        }        
        TreeViewHelper.sortItems(parent_node.children)
      } else {
        Vue.prototype.$dialog.notify.error(
          "程序内部错误，无法找到项目节点" + item.parent_id
        )
      }
    }
  }
  deleteItem(item) {
    // 传入的item要有id和parent_id即可
    if (item.parent_id === this.top_level_parent_id) {
      // 说明是顶层的目录
      const item_index = this.top_level_items.findIndex((x) => x.id === item.id)
      if (item_index > -1) {
        this.top_level_items.splice(item_index, 1)
      }
    } else {
      const parent_node = this.findItemById(item.parent_id)
      if (parent_node) {
        const parent_item_index = parent_node.children.findIndex(
          (x) => x.id === item.id
        )
        if (parent_item_index > -1) {
          parent_node.children.splice(parent_item_index, 1)
          if(parent_node.children.length==0){
            // tell dom to delete children
            Vue.delete(parent_node, 'children')
          }
        }
      } else {
        Vue.prototype.$dialog.notify.error(
          "程序内部错误，无法找到项目节点" + item.parent_id
        )
      }
    }
  }
}

export default TreeViewHelper
export { TreeViewHelper, ITreeViewItem }
